package com.itheima.网络编程.udp3;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

public class 第一例udp服务端 {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        while (true) {
            DatagramSocket ds = new DatagramSocket(10001);
            byte[] bys = new byte[1024 * 1024];
            DatagramPacket dgp = new DatagramPacket(bys, bys.length);
            System.out.println("服务端接收到的数据:");
            ds.receive(dgp);
            System.out.println(new String(dgp.getData(), 0, dgp.getLength()));
            ds.close();
            System.out.println("=====================================================");
            DatagramSocket ds1 = new DatagramSocket();
            System.out.println("服务端请输入要发给客户端的数据:");
            byte[] bys1 = sc.next().getBytes();
            DatagramPacket dgp1 = new DatagramPacket(bys1, bys1.length, InetAddress.getByName("127.0.0.1"), 10002);
            ds1.send(dgp1);
            ds1.close();
            System.out.println("=====================================================");


        }

    }

}
